<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/14
 * Time: 15:58
 */



namespace app\http\middleware;

use app\admin\model\Menu;
use app\Request;


class AuthVerify
{
    protected $auth_role = [
        '/auth/user','/home/orderstatistics','/user/userinfo','/coupon/usercouponlist',
        '/order/createGoodsOrder','/order/createPackageOrder','/order/paymentCodeGoods',
        '/package/packagelist','/goods/goodslist','/goods/gooodsdiscount',
        '/home/amountstatistics','/home/paywaystatistics','/home/packagestatistics',
        '/home/amounttotal', '/home/financestatistics','/home/payway','/home/statistics',
        '/home/upload','/home/rechargestatistics','/package/discount','/package/usePackage'


        ];
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle(Request $request, \Closure $next)
    {

        $menuModel =  new Menu();
        $roleId  = array_column($request->role,'role_id');
        $menu = $menuModel->getMenu($roleId);
        /*if (empty($menu)){
            return adminError("账号无权限",'403');
        }
        $url ='/'. $request->rule()->getRule();

        if (!in_array(strtolower($url),$this->auth_role) ){

            $menuPath = array_column($menu,'path');
            if (!in_array(strtolower($url),array_map('strtolower', $menuPath))){
                return adminError("账号无权限",'403');
            }
        }*/
        foreach ($menu as $key => $value){
            if ($value['menu_type'] != 0){
                unset($menu[$key]);
            }
        }
        $request->menu = array_values($menu);
        $response = $next($request);

        return $response;
    }


}